package com.aididi.interceptor;

import com.aididi.entity.FastPrincipal;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.simp.stomp.StompCommand;
import org.springframework.messaging.simp.stomp.StompHeaderAccessor;
import org.springframework.messaging.support.ChannelInterceptor;
import org.springframework.messaging.support.MessageHeaderAccessor;

/**
 * @Auther: zx.xu
 * @Description:
 */
public class UserInterceptor implements ChannelInterceptor{

    /**
     * 获取包含在stomp中的用户信息
     */
    @Override
    public Message<?> preSend(Message<?> message, MessageChannel channel) {
        StompHeaderAccessor accessor = MessageHeaderAccessor.getAccessor(message,StompHeaderAccessor.class);
        if(StompCommand.CONNECT.equals(accessor.getCommand())){
            String userName = accessor.getNativeHeader("name").get(0);
            accessor.setUser(new FastPrincipal(userName));
        }
        return null;
    }
}
